﻿using System.Collections.Generic;
using System.Linq;

using Common.Base;
using Common.Enums;

using GameCommon.Base;
using GameCommon.ComponentInterfaces;
using GameCommon.Manager;
using GameCommon.ManagerInterfaces;
using GameCommon.Services;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using GameComponent = GameCommon.Components.GameComponent;

namespace GameGraphics.Components
{
    public class MessageBox : GameComponent, IHitable
    {
        private const int LifeTime = 100;

        private readonly Offset off = new Offset(5, 5, 5, 5);

        private readonly List<Message> messages;

        private readonly int messageCount;

        private readonly Texture2D background;

        private float backgroundOpacity;

        public MessageBox(string id, FontSizeEnum fontSize, FontEnum fontEnum, int messageCount)
            : base(id)
        {
            this.messageCount = messageCount;
            this.background = Managers.ContentManager.GetRectangle(Color.Black);
            this.messages = new List<Message>();
            this.Font = Managers.FontManager.GetFont(fontEnum, fontSize);
            this.Layer = 5;
        }
        
        public MessageBox(string id, FontSizeEnum fontSize, FontEnum fontEnum)
            : base(id)
        {
            this.background = Managers.ContentManager.GetRectangle(Color.Black);
            this.messageCount = 5;
            this.messages = new List<Message>();
            this.Font = Managers.FontManager.GetFont(fontEnum, fontSize);
            this.Layer = 5;
        }

        public IHitBox HitBox { get; set; }

        public Font Font { get; set; }

        public void Update()
        {
            this.backgroundOpacity = this.messages.Count > 0 ? 0.5f : 0.2f;
            this.backgroundOpacity = this.HitBox.IsMouseOver ? 0.7f : this.backgroundOpacity;

            foreach (var message in this.messages)
            {
                message.ReduceLifeTime(1);
            }

            this.messages.RemoveAll(x => x.Lifetime == 0);
        }

        public override void Draw(IDrawingManager manager, GameTime gameTime)
        {
            Service.DrawingService.Draw(manager, this.background, this.Dimension, this.backgroundOpacity, this.Layer);

            var bottomLeft = this.Dimension.Y + this.Dimension.Height;
            
            for (var i = 0; i < this.messages.Count && i < this.messageCount; i++)
            {
                var text = this.messages[i].Text;
                var size = Service.FontService.GetSize(text, this.Font);

                var y = bottomLeft - ((i + 1) * size.Height) - this.off.Bottom;
                var x = this.Dimension.X + this.off.Left;

                var dim = new Dimension(x, y);
                Service.DrawingService.DrawFont(manager, this.Font, dim, text, 1, this.Layer + 1);
            }
        }

        public void Add(string message, ResultTypeEnum type)
        {
            if (this.messages.Count == 5)
            {
                this.messages.Remove(this.messages.Last());
            }

            this.messages.Add(new Message(message, type, LifeTime));
        }

        public void Add(Result result)
        {
            this.Add(result.Message, result.ResultType);
        }
    }
}